{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import pandas as pd\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [],
   "source": [
    "XSRF_TOKEN = '82c8bd'\n",
    "mweibo_short_token = '82cec0dee7'"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "outputs": [],
   "source": [
    "tbasedata = pd.read_csv('../data/basedata.csv')\n",
    "basedata = tbasedata.head(10)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "outputs": [],
   "source": [
    "# resp = getReqFromUid('5492965861')\n",
    "#     #解析请求结果\n",
    "# weibo_data = json.loads(resp.text)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [],
   "source": [
    "def getReqFromUid(uid):\n",
    "    url = \"https://m.weibo.cn/api/attentionvist/tagUsersCounts?to_uid=\"+uid\n",
    "    headers = {\n",
    "    # 'X-XSRF-TOKEN': XSRF_TOKEN ,\n",
    "    'User-Agent': 'Mozilla/5.0 (iPad; CPU OS 17_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko)  Mobile/15E148 Weibo (iPad13,16__weibo__13.12.0__ipad__os17.1.2)',\n",
    "    'Accept-Language': 'zh-CN,zh-Hans;q=0.9',\n",
    "    'Host': 'm.weibo.cn',\n",
    "    'X-Requested-With': 'XMLHttpRequest',\n",
    "    'Sec-Fetch-Site': 'same-origin',\n",
    "    'Accept-Encoding': 'gzip, deflate, br',\n",
    "    'Sec-Fetch-Mode': 'cors',\n",
    "    'Accept': 'application/json, text/plain, */*',\n",
    "    'Referer': 'https://m.weibo.cn/c/attention/visitList?type=field&sign='+uid+'&tagId=1042015%253AtagCategory_050',\n",
    "    'Connection': 'keep-alive',\n",
    "    'Sec-Fetch-Dest': 'empty'\n",
    "    }\n",
    "    cookies = {\n",
    "        # 'XSRF-TOKEN': XSRF_TOKEN,\n",
    "        'MLOGIN':'1',\n",
    "        'M_WEIBOCN_PARAMS':'from%3D10DC099020',\n",
    "        # 'mweibo_short_token':mweibo_short_token,\n",
    "        'WEIBOCN_FROM':'10DC099020',\n",
    "        '_T_WM':'17410080410',\n",
    "        'SCF':'As4aMApLFh3GjHrHtP-TAw1nrLE46kFu5I5r0MB3BTzPNoySRgKVJlVhMNFb_SQR6g..','SUB':'_2A25IfAIIDeRhGeBK7loX8SvJzDuIHXVr7khArDV6PUJbitAGLRnBkWtNR7wuN0ag2MQHqt7bIr3LIvZwrAJhJl9u',\n",
    "        'SUBP':'0033WrSXqPxfM725Ws9jqgMF55529P9D9Whf0DnOrDhQZ_cgNMTEyiM.5NHD95QcSh-RSo2fSKMNWs4Dqc_Ui--Ni-i8iKy8i--Xi-zRi-i2i--NiKLWiKnXi--Ni-iFi-zfi--Ni-iFi-zfi--Ri-2ciKnpi--NiK.Ri-2ci--ciKnRiK.7i--4iK.Ni-8Wi--NiK.Xi-zfSKe7'\n",
    "\n",
    "\n",
    "    }\n",
    "    response = requests.get(url, headers=headers, cookies=cookies)\n",
    "    return response"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\DelandPeng\\AppData\\Local\\Temp\\ipykernel_8308\\175288534.py:30: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
      "  bddPD = bddPD.append(testpd,ignore_index=True)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    },
    {
     "data": {
      "text/plain": "  MBTI类型        UserName      UserID      粉丝数   关注数  性别（女0男1）       注册日期  \\\n0   ISFJ        lymr2012  1330417035       71   276         0  2012/9/12   \n1   ISFJ          西红柿妹妹_  1679622967      256   313         0   2011/6/6   \n2   ISFJ           落月入星河  1692504434      416   186         0  2010/2/10   \n3   ISFJ           寻小Mei  1746598137  1030000   272         0  2010/5/25   \n4   ISFJ            一头罗勒  2270723560      656   890         1  2011/7/27   \n5   ISFJ          四夕____  2972826390      618   714         0  2012/8/27   \n6   ISFJ  Lari_LoveRiGyu  3053407104    15000   598         0   2013/1/1   \n7   ISFJ         芭乐椰椰小泡芙  3180284600     1506  1187         0   2013/2/5   \n8   ISFJ          我就是好奇啊  3721317740     3271   537         0  2013/8/19   \n9   ISFJ             K小澜  3735395440      191   595         0  2013/8/21   \n\n     微博数       互动数  昨日阅读数     视频累积播放量                    top5  \\\n0    655     109.0    NaN         NaN  娱乐明星+音乐演出+情感生活+搞笑幽默+颜值   \n1    432     608.0    NaN      4051.0      娱乐明星+音乐演出+颜值+时尚+美妆   \n2  20914    9186.0    NaN     13000.0   娱乐明星+音乐演出+教育+电视剧+读书作家   \n3   1280  725081.0    NaN  43357000.0      美妆+时尚+搞笑幽默+娱乐明星+颜值   \n4  12802   44718.0    NaN    518000.0  情感生活+音乐演出+搞笑幽默+颜值+娱乐明星   \n5   6585   19745.0    NaN      2854.0    音乐演出+娱乐明星+动漫+搞笑幽默+美食   \n6   6737   22051.0    NaN     74000.0   电视剧+娱乐明星+音乐演出+游戏+搞笑幽默   \n7    649    7270.0    NaN      2631.0      情感生活+颜值+娱乐明星+美妆+时尚   \n8   9150   37384.0    NaN    212000.0   娱乐明星+电视剧+体育+综艺节目+社会时事   \n9   6914    6181.0    NaN     31000.0      娱乐明星+音乐演出+美食+电影+颜值   \n\n                                                喜好列表  \n0  娱乐明星+音乐演出+情感生活+搞笑幽默+颜值+动物宠物+美食+综艺节目+美妆+电视剧+数码+...  \n1  娱乐明星+音乐演出+颜值+时尚+美妆+运动健身+搞笑幽默+动漫+美食+游戏+情感生活+动物宠...  \n2  娱乐明星+音乐演出+教育+电视剧+读书作家+搞笑幽默+动漫+科学科普+情感生活+健康医疗+社...  \n3  美妆+时尚+搞笑幽默+娱乐明星+颜值+音乐演出+美食+旅游出行+动物宠物+电视剧+摄影拍照+...  \n4  情感生活+音乐演出+搞笑幽默+颜值+娱乐明星+运动健身+游戏+电影+时尚+动漫+科学科普+美...  \n5  音乐演出+娱乐明星+动漫+搞笑幽默+美食+时尚+教育+美妆+情感生活+读书作家+颜值+电影+...  \n6      电视剧+娱乐明星+音乐演出+游戏+搞笑幽默+综艺节目+时尚+动漫+社会时事+颜值+情感生活  \n7  情感生活+颜值+娱乐明星+美妆+时尚+音乐演出+读书作家+搞笑幽默+教育+美食+动漫+摄影拍...  \n8  娱乐明星+电视剧+体育+综艺节目+社会时事+游戏+音乐演出+搞笑幽默+电影+科学科普+旅游出...  \n9  娱乐明星+音乐演出+美食+电影+颜值+美妆+搞笑幽默+体育+电视剧+教育+动漫+综艺节目+游...  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>MBTI类型</th>\n      <th>UserName</th>\n      <th>UserID</th>\n      <th>粉丝数</th>\n      <th>关注数</th>\n      <th>性别（女0男1）</th>\n      <th>注册日期</th>\n      <th>微博数</th>\n      <th>互动数</th>\n      <th>昨日阅读数</th>\n      <th>视频累积播放量</th>\n      <th>top5</th>\n      <th>喜好列表</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>ISFJ</td>\n      <td>lymr2012</td>\n      <td>1330417035</td>\n      <td>71</td>\n      <td>276</td>\n      <td>0</td>\n      <td>2012/9/12</td>\n      <td>655</td>\n      <td>109.0</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>娱乐明星+音乐演出+情感生活+搞笑幽默+颜值</td>\n      <td>娱乐明星+音乐演出+情感生活+搞笑幽默+颜值+动物宠物+美食+综艺节目+美妆+电视剧+数码+...</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>ISFJ</td>\n      <td>西红柿妹妹_</td>\n      <td>1679622967</td>\n      <td>256</td>\n      <td>313</td>\n      <td>0</td>\n      <td>2011/6/6</td>\n      <td>432</td>\n      <td>608.0</td>\n      <td>NaN</td>\n      <td>4051.0</td>\n      <td>娱乐明星+音乐演出+颜值+时尚+美妆</td>\n      <td>娱乐明星+音乐演出+颜值+时尚+美妆+运动健身+搞笑幽默+动漫+美食+游戏+情感生活+动物宠...</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>ISFJ</td>\n      <td>落月入星河</td>\n      <td>1692504434</td>\n      <td>416</td>\n      <td>186</td>\n      <td>0</td>\n      <td>2010/2/10</td>\n      <td>20914</td>\n      <td>9186.0</td>\n      <td>NaN</td>\n      <td>13000.0</td>\n      <td>娱乐明星+音乐演出+教育+电视剧+读书作家</td>\n      <td>娱乐明星+音乐演出+教育+电视剧+读书作家+搞笑幽默+动漫+科学科普+情感生活+健康医疗+社...</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>ISFJ</td>\n      <td>寻小Mei</td>\n      <td>1746598137</td>\n      <td>1030000</td>\n      <td>272</td>\n      <td>0</td>\n      <td>2010/5/25</td>\n      <td>1280</td>\n      <td>725081.0</td>\n      <td>NaN</td>\n      <td>43357000.0</td>\n      <td>美妆+时尚+搞笑幽默+娱乐明星+颜值</td>\n      <td>美妆+时尚+搞笑幽默+娱乐明星+颜值+音乐演出+美食+旅游出行+动物宠物+电视剧+摄影拍照+...</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>ISFJ</td>\n      <td>一头罗勒</td>\n      <td>2270723560</td>\n      <td>656</td>\n      <td>890</td>\n      <td>1</td>\n      <td>2011/7/27</td>\n      <td>12802</td>\n      <td>44718.0</td>\n      <td>NaN</td>\n      <td>518000.0</td>\n      <td>情感生活+音乐演出+搞笑幽默+颜值+娱乐明星</td>\n      <td>情感生活+音乐演出+搞笑幽默+颜值+娱乐明星+运动健身+游戏+电影+时尚+动漫+科学科普+美...</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>ISFJ</td>\n      <td>四夕____</td>\n      <td>2972826390</td>\n      <td>618</td>\n      <td>714</td>\n      <td>0</td>\n      <td>2012/8/27</td>\n      <td>6585</td>\n      <td>19745.0</td>\n      <td>NaN</td>\n      <td>2854.0</td>\n      <td>音乐演出+娱乐明星+动漫+搞笑幽默+美食</td>\n      <td>音乐演出+娱乐明星+动漫+搞笑幽默+美食+时尚+教育+美妆+情感生活+读书作家+颜值+电影+...</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>ISFJ</td>\n      <td>Lari_LoveRiGyu</td>\n      <td>3053407104</td>\n      <td>15000</td>\n      <td>598</td>\n      <td>0</td>\n      <td>2013/1/1</td>\n      <td>6737</td>\n      <td>22051.0</td>\n      <td>NaN</td>\n      <td>74000.0</td>\n      <td>电视剧+娱乐明星+音乐演出+游戏+搞笑幽默</td>\n      <td>电视剧+娱乐明星+音乐演出+游戏+搞笑幽默+综艺节目+时尚+动漫+社会时事+颜值+情感生活</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>ISFJ</td>\n      <td>芭乐椰椰小泡芙</td>\n      <td>3180284600</td>\n      <td>1506</td>\n      <td>1187</td>\n      <td>0</td>\n      <td>2013/2/5</td>\n      <td>649</td>\n      <td>7270.0</td>\n      <td>NaN</td>\n      <td>2631.0</td>\n      <td>情感生活+颜值+娱乐明星+美妆+时尚</td>\n      <td>情感生活+颜值+娱乐明星+美妆+时尚+音乐演出+读书作家+搞笑幽默+教育+美食+动漫+摄影拍...</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>ISFJ</td>\n      <td>我就是好奇啊</td>\n      <td>3721317740</td>\n      <td>3271</td>\n      <td>537</td>\n      <td>0</td>\n      <td>2013/8/19</td>\n      <td>9150</td>\n      <td>37384.0</td>\n      <td>NaN</td>\n      <td>212000.0</td>\n      <td>娱乐明星+电视剧+体育+综艺节目+社会时事</td>\n      <td>娱乐明星+电视剧+体育+综艺节目+社会时事+游戏+音乐演出+搞笑幽默+电影+科学科普+旅游出...</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>ISFJ</td>\n      <td>K小澜</td>\n      <td>3735395440</td>\n      <td>191</td>\n      <td>595</td>\n      <td>0</td>\n      <td>2013/8/21</td>\n      <td>6914</td>\n      <td>6181.0</td>\n      <td>NaN</td>\n      <td>31000.0</td>\n      <td>娱乐明星+音乐演出+美食+电影+颜值</td>\n      <td>娱乐明星+音乐演出+美食+电影+颜值+美妆+搞笑幽默+体育+电视剧+教育+动漫+综艺节目+游...</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idList = basedata['UserID']\n",
    "bddPD = pd.DataFrame()\n",
    "for i in range(len(idList)):\n",
    "    print(i)\n",
    "    uid = str(idList[i])\n",
    "    #发起请求\n",
    "    try:\n",
    "        resp = getReqFromUid(uid)\n",
    "    except Exception as e:\n",
    "        print(e)\n",
    "        testpd = pd.DataFrame({'UserID':[int(uid)],'top5':['手动拉取'],'喜好列表':['手动拉取']})\n",
    "        bddPD = bddPD.append(testpd,ignore_index=True)\n",
    "    else:\n",
    "        #解析请求结果\n",
    "        weibo_data = json.loads(resp.text)\n",
    "        if weibo_data.get('data'):\n",
    "            tag_list = weibo_data['data']['tag_users']\n",
    "            tagstr = ''\n",
    "            tagtopfive = ''\n",
    "            for j in range(len(tag_list)):\n",
    "                tag = tag_list[j]\n",
    "                if tagstr =='':\n",
    "                    tagstr = tag['tag_name']\n",
    "                    tagtopfive = tag['tag_name']\n",
    "                else:\n",
    "                    if j<5:\n",
    "                        tagtopfive += '+'+tag['tag_name']\n",
    "                    tagstr += '+'+tag['tag_name']\n",
    "            testpd = pd.DataFrame({'UserID':[int(uid)],'top5':[tagtopfive],'喜好列表':[tagstr]})\n",
    "            bddPD = bddPD.append(testpd,ignore_index=True)\n",
    "\n",
    "        else:\n",
    "            testpd = pd.DataFrame({'UserID':[int(uid)],'top5':['手动拉取'],'喜好列表':['手动拉取']})\n",
    "            bddPD = bddPD.append(testpd,ignore_index=True)\n",
    "        time.sleep(0.3)\n",
    "\n",
    "basedata  = pd.merge(basedata,bddPD,on=['UserID'])\n",
    "basedata.head(10)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}